package com.cinema;
import java.util.ArrayList;
import java.util.Date;

public class DataManager {
    private static final String USER_FILE = "users.txt";
    private static final String MOVIE_FILE = "movies.txt";
    private static final String SCHEDULE_FILE = "schedules.txt";
    private static final String TICKET_FILE = "tickets.txt";
    private static final String LOG_FILE = "logs.txt";

    public static ArrayList<User> userList = new ArrayList<>();
    public static ArrayList<Movie> movieList = new ArrayList<>();
    public static ArrayList<Schedule> scheduleList = new ArrayList<>();
    public static ArrayList<Ticket> ticketList = new ArrayList<>();
    public static ArrayList<String> logList = new ArrayList<>();

    public static void init() {
        userList = FileUtil.readFromFile(USER_FILE);
        movieList = FileUtil.readFromFile(MOVIE_FILE);
        scheduleList = FileUtil.readFromFile(SCHEDULE_FILE);
        ticketList = FileUtil.readFromFile(TICKET_FILE);
        logList = FileUtil.readFromFile(LOG_FILE);

        if (userList.stream().noneMatch(u -> u.getUsername().equals("admin"))) {
            User admin = new Admin("AD001", "admin", MD5Util.encrypt("ynuinfo#777"), new Date(), "12345678901", "管理员");
            userList.add(admin);
        }
    }

    public static void saveData() {
        FileUtil.saveToFile(userList, USER_FILE);
        FileUtil.saveToFile(movieList, MOVIE_FILE);
        FileUtil.saveToFile(scheduleList, SCHEDULE_FILE);
        FileUtil.saveToFile(ticketList, TICKET_FILE);
        FileUtil.saveToFile(logList, LOG_FILE);
        System.out.println("数据已保存");
    }
}